import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/Home.vue'

const originalPush = VueRouter.prototype.push;
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err)
};
Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'Home',
    component: Home,
    children: [{
      path: '/home/search',
      name: 'search',
      component: () => import('../views/Searchpopus.vue')
    }]
  },
  {
    path: '/topic',
    name: 'topic',
    component: () => import('@/views/tabbar/topic.vue')
  },
  {
    path: '/classification',
    name: 'classification',
    component: () => import('@/views/tabbar/classification.vue'),
    children: [{
      path: '/classification/search',
      name: 'search',
      component: () => import('../views/Searchpopus.vue')
    }],
  },
  {
    path: '/gcar',
    name: 'gcar',
    component: () => import('@/views/tabbar/gcar.vue')
  },
  {
    path: '/person',
    name: 'person',
    component: () => import('@/views/tabbar/person.vue')
  },
  {
    path: '/goodsDetail',
    name: 'goodsDetail',
    component: () => import('@/components/buy/goodsDetail.vue'),
  },
  {
    path: '/Tabnav',
    name: 'Tabnav',
    component: () => import('@/components/navTab/Tabnav.vue'),
  },
  {
    path: '/tabData',
    name: 'tabData',
    component: () => import('@/components/navTab/tabData.vue'),
  },
  {
    path: '/brand',
    name: 'brand',
    component: () => import('@/components/brand/brand.vue'),
  },

]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})
// 路由守卫
router.beforeEach((to, from, next) => {
  // console.log('to:',to);
  // console.log('from:',from);
  // let token=false;
  if (to.path == '/gcar') {
    if (localStorage.getItem('tok')) {
      next();
    } else {
      setTimeout(() => {
        // alert('请先登录')
        next('/person')
      }, 1000)
    }
    return
  }
  next()
})


export default router
